// pages/confirmOrder/confirmOrder.js
// 获取应用实例
import request from '../../utils/request'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    grap: [],
    allData: [],
    confirmOrderData: [],
    id: "",
    coupon:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 跳转确认支付
    submitOrder() {
      let time = new Date().getTime() + 1000*60*60
      wx.navigateTo({
        url: `/pages/patternPayment/patternPayment?id=${this.data.id}&time=${time}`,
      })
    },
    async getCoupon(params) {
      await request('/users/coupon', params, {}, "GET").then((res) => {
        this.setData({
          coupon: [res[0]]
        })
      })
    },
    onLoad: function (option) {
      // 获取数据
      this.getData()
      // 获取优惠券
      this.getCoupon()
      this.getTeacherClassData(option.id)
      this.setData({
        id: option.id
      })
      const eventChannel = this.getOpenerEventChannel()
      eventChannel.emit('acceptDataFromOpenedPage', {
        data: 'test'
      });
      eventChannel.emit('someEvent', {
        data: 'test'
      });
      // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
      eventChannel.on('acceptDataFromOpenerPage', function (data) {
        console.log(data)
      })
    },
    // 总数据
    async getData(params) {
      await request('/users/lecture', params, {}, "GET").then((res) => {
        this.setData({
          grap: res
        })
      })
      let grap = this.data.grap.map((item) => {
        item.position = JSON.parse(item.position)
        item.teacherClassList = JSON.parse(item.teacherClassList)
        return item
      })
      // 总数据
      this.setData({
        grap
      })
      console.log(this.data.grap);
    },
    // 获取teacherClassList 所有数据
    async getTeacherClassData(id, params) {
      await request('/users/lecture', params, {}, 'GET').then(res => {
        res.map((item) => {
          item.teacherClassList = JSON.parse(item.teacherClassList)
        })

        let arr = [];
        res.forEach((item) => {
          arr.push(...item.teacherClassList)
        })
        this.setData({
          allData: arr
        })
        let obj = this.data.allData.find(item => {
          item.num = 1
          return item.cid === id
        })
        this.setData({
          confirmOrderData: obj
        })
      })
    },
    
    // 获取优惠券
    onSale(){
      wx.navigateTo({
        url: `/pages/onSale/onSale?classPrice=${this.data.confirmOrderData.classPrice}`,
      })
    }
  }
})